import ssl
import requests
from requests.adapters import HTTPAdapter, PoolManager
from requests.auth import HTTPBasicAuth
from urllib3.exceptions import InsecureRequestWarning
from urllib3 import disable_warnings


# 使用http方式调用官方提供的restconf接口获取信息，需要在防火墙的管理中进行北向配置，开放restconf接口
class MyAdapter(HTTPAdapter):
    # 重写init_poolmanager方法，使用tls1.2。
    def init_poolmanager(self, connections, maxsize, block=False, **pool_kwargs):
        self.poolmanager = PoolManager(num_pools=connections,
                                       maxsize=maxsize,
                                       block=block,
                                       ssl_version=ssl.PROTOCOL_TLSv1_2)


# 查询健康检测的状态
url = 'https://192.168.12.12:8447/restconf/data/huawei-healthcheck:healthcheck?content=all'
header = {
    'Host': 'ip:8447',
    'Accept': '*/*'
}

disable_warnings(InsecureRequestWarning)

# 定义登录账户密码
basic = HTTPBasicAuth('admin', 'Tontron@1169')
s = requests.Session()

# 调用重写的方法
s.mount('https://', MyAdapter())

r = s.get(url=url, headers=header, auth=basic, verify=False)

print(r.request.headers)
print(r.status_code)
print(r.text)
s.close()